package com.framework.wz.wutong.base;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

import com.framework.wz.wutong.R;
import com.framework.wz.wutong.dialog.ProgressHUD;
import com.framework.wz.wutong.utils.StatusBarUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;

import butterknife.ButterKnife;
import butterknife.Unbinder;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;

public abstract class BaseActivity extends AppCompatActivity{

    private ProgressHUD         mProgressHUD;
    private ArrayList<String>           tagList;
    private CompositeDisposable mCompositeDisposable;
    private Unbinder unbinder;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(attachLayoutRes());
        unbinder = ButterKnife.bind(this);
        if (useEventBus()) {
            Log.e("123", "111111111111111");
            EventBus.getDefault().register(this);
        }
        mCompositeDisposable = new CompositeDisposable();
        tagList = new ArrayList<>();
        initData();
        initView();
        initListener();
    }

    /**
     * 初始化布局id
     */
    protected abstract int attachLayoutRes();

    /**
     * 初始化View
     */
    protected abstract void initView();

    /**
     * 初始化数据
     */
    protected abstract void initData();

    /**
     * 初始化监听器
     */
    protected abstract void initListener();

    protected boolean useEventBus(){
        return false;
    }

    /**
     * 加载提示框
     */
    protected void showDialog(boolean show) {
        if (show) {
            tagList.add("tag");
            if (mProgressHUD == null) {
                mProgressHUD = ProgressHUD.show(this, getResources().getString(R.string.dialog_show), true, false, null);
            } else {
                if (mProgressHUD.isShowing())
                mProgressHUD.show();
            }
        } else {
            if (mProgressHUD != null) {
                switch (tagList.size()) {
                    case 0:{
                        mProgressHUD.dismiss();
                        break;
                    }
                    case 1: {
                        tagList.remove(0);
                        mProgressHUD.dismiss();
                        break;
                    }
                    default: {
                        tagList.remove(0);
                    }
                }
            }
        }
    }

    protected void addSubscription(Disposable disposable) {
        if (!disposable.isDisposed()) {
            mCompositeDisposable.add(disposable);
        }
    }

    private void unDispose() {
        if (mCompositeDisposable != null) {
            mCompositeDisposable.clear();
        }
        mCompositeDisposable = null;
    }

    /**
     * eventbus回调方法
     *
     * @param intent
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(Intent intent) {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (unbinder != null) {
            unbinder.unbind();
        }
        unDispose();
        EventBus.getDefault().unregister(this);
    }
}
